import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Home from '../components/Home.vue'
import BrandList from '@/components/BrandList.vue'
import Login from '@/components/Login.vue'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'home',
    component: Home,
    children: [{
        path: '/about',
        name: 'about',
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/AboutView.vue')
    }, {
        path: '/brandList',
        name: 'brandList',
        component: BrandList,
    }]
}, {
    path: '/login',
    name: 'login',
    component: Login,
}]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    console.log(to, from);
    if (to.name === 'login') {
        // 放行
        next()
    } else {
        const token = localStorage.getItem('token')
        if (token) {
            // 进行放行
            next()
        } else {
            router.push("/login")
            return
        }
        // 放行
        next()
    }
})

export default router